﻿using PlatForm.DataLayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PlatForm.Controller
{
    public class PickingController
    {
        public List<Picking> GetLack(DateTime d)
        {
            var lstPick = Picking.Objects.Where(x => x.PickDate == d.Date).ToList();
            var lstPickNo = lstPick.Select(x => x.PIK_No);

            var lstPickDetail = PickingDetail.Objects.Where(x => lstPickNo.Contains(x.PIK_No)).ToList();

            var lstOrderNo = lstPick.Select(x => x.ORD_No).ToList();

            var lstOrder = Order.Objects.Where(x=>lstOrderNo.Contains(x.ORD_No)).ToList();
            var lstArticleNo = lstOrder.Select(x => x.ART_No).ToList();

            var lstArticle = Article.Objects.Where(x => lstArticleNo.Contains(x.ART_No)).ToList();
            var lstSupplierNo = lstPick.Select(x => x.SUPPL_No).ToList();

            var lstSupplier = Supplier.Objects.Where(x => lstSupplierNo.Contains(x.SUPPL_No)).ToList();
            

            foreach (var s in lstPick)
            {
                var f = lstSupplier.FirstOrDefault(x => x.SUPPL_No == s.SUPPL_No);
                if (f != null)
                    s.SUPPL_Name = f.SUPPL_Name;
                var a = lstArticle.FirstOrDefault(x => x.ART_No == s.ART_No);
                if (a != null)
                    s.ART_Name = a.ART_Name;
                var o = lstOrder.FirstOrDefault(x => x.ORD_No == s.ORD_No);
                if (o != null)
                    s.ORD_QTY = o.DELI_QTY;
                s.NaturalLoss = lstPickDetail.Sum(x => x.NaturalLoss);
                s.ProcessLoss = lstPickDetail.Sum(x => x.ProcessLoss);
                s.DifferKg = s.DELI_QTY - s.ORD_QTY;
                s.DifferPc = s.DifferKg/s.ORD_QTY*100;
                s.Status = s.DELI_QTY >= s.ORD_QTY;
            }
            return lstPick;
        }

        public List<Picking> GetPick(DateTime d)
        {
            var lstPick = Picking.Objects.Where(x => x.PickDate == d.Date && x.Status==false).ToList();
            var lstPickNo = lstPick.Select(x => x.PIK_No);

            var lstPickDetail = PickingDetail.Objects.Where(x => lstPickNo.Contains(x.PIK_No)).ToList();

            var lstOrderNo = lstPick.Select(x => x.ORD_No).ToList();

            var lstOrder = Order.Objects.Where(x => lstOrderNo.Contains(x.ORD_No)).ToList();
     
            foreach (var s in lstPick)
            {
                var o = lstOrder.FirstOrDefault(x => x.ORD_No == s.ORD_No);
                if (o != null)
                    s.ORD_QTY = o.DELI_QTY;
                s.NaturalLoss = lstPickDetail.Sum(x => x.NaturalLoss);
                s.ProcessLoss = lstPickDetail.Sum(x => x.ProcessLoss);
                var lst = lstPickDetail.Where(x => x.PIK_No == s.PIK_No).ToList();
                s.Package = lst.Sum(x => x.Lon) + lst.Sum(x => x.Trung) + lst.Sum(x => x.Dau) + lst.Sum(x => x.Be);
            }
            return lstPick;
        }
    }
}
